define(['jquery', 'bootstrap', 'userend', 'table', 'form', 'editable'], function ($, undefined, Userend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            var params = '/goods_id/' + Config.goods_id + '/cat_ids/' + Config.cat_ids;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/goods/specitem/index' + params,
                    add_url: 'mall/goods/specitem/add' + params,
                    edit_url: 'mall/goods/specitem/edit' + params,
                    del_url: 'mall/goods/specitem/del' + params,
                    multi_url: 'mall/goods/specitem/multi' + params,
                    table: 'mall_goods_specitem',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'itemId',
                //sortName: 'itemId',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'goods_id', title: __('Goods_id'), visible:false, formatter: Table.api.formatter.search},
                        {field: 'goods_id_text', title: __('Goods_id')},
                        {field: 'spec_id_text', title: __('Spec_id')},
                        {field: 'itemId', title: "项ID"},
                        {field: 'spec_id', title: __('Spec_id'), visible:false},
                        {field: 'itemname', title: __('Itemname'), editable: true},
                        {field: 'itemdesc', title: __('Itemdesc'), visible:false},
                        {field: 'allowimgis', title: __('Allowimgis'), visible:false},
                        {field: 'itemimg', title: __('Itemimg'), formatter: Table.api.formatter.image},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [{
                                name: 'add',
                                icon: 'fa fa-plus',
                                title: __('Add'),
                                extend: 'data-toggle="tooltip"',
                                classname: 'btn btn-xs btn-info btn-add btn-dialog',
                                url: $.fn.bootstrapTable.defaults.extend.add_url + "/spec_id/{spec_id}/allowimgis/{allowimgis}",
                            }],
                            formatter:function (value, row, index) {
                                if(row.itemId){
                                    return Table.api.formatter.operate.call(this, value, row, index);
                                }else{
                                    return Table.api.formatter.buttons.call(this, value, row, index);
                                }
                            }
                        }
                    ]
                ],
                pagination: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $("#c-spec_id").data("params", function (obj) {
                    return {custom: {cat_id: $("#spec_ids").val()}};
                });
            },
        }
    };
    return Controller;
});